package boot.spring.算法设计与分析.分治法.汉诺塔;

import java.util.Arrays;

public class Hanoi {

    public static void main(String[] args) {
        int n = 4;
        String[] a = { "1","2","3","4"};
        String[] b = { "", "", "" , "" } ;
        String[] c = { "", "", "", ""  } ;
        hanoi(n, a, b ,c);

        System.out.println("a: "+ Arrays.asList(a));
        System.out.println("b: "+ Arrays.asList(b));
        System.out.println("c: "+ Arrays.asList(c));


    }

    /**
     * 将n个盘子从a通过b移动到c
     * @param n
     * @param a
     * @param b
     * @param c
     */
    public static void hanoi(int n , String[] a, String[] b , String[] c) {
        if (n >1){
            //  将先将前n-1个盘子从a通过b移动到c
            System.out.println("第 【"+n+"】 轮--------将先将前 " +(n-1)+ " 个盘子从A通过b移动到C");
            hanoi(n-1,a,c ,b);
            // 将第n个盘子从a直接移动到c
            move(n ,a,c);
            //  再将先将前n-1个盘子从b通过a移动到c
            System.out.println("第 【"+n+"】 轮--------再将先将前 "+(n-1)+" 个盘子从B通过a移动到C");
            hanoi(n-1,b,a ,c);
        }else{
            move(n ,a, c);
        }
    }


    public static void move(int n , String[] a, String[] c) {
        String temp = a[n-1];
        c[n-1] =  temp;
        a[n-1] = "";
        System.out.println("将第 "+n+" 个盘子（"+temp+"）从a直接移动到c");
        System.out.println("此时，a盘子："+ Arrays.asList(a)+" ,c盘子："+ Arrays.asList(c));
    }
}
